package Frame.battleanimation;

import FCRendering.FCRConstants;
import FCRendering.FCRMaster;
import VWorldGameStudio.FCAnimation;
import VWorldGameStudio.FCItem;

import java.awt.*;

import static FCRendering.FCRMaster.*;

//投掷道具的
public class ThrowItemAni extends ABattleAni implements IBattleAni {

    //原点
    protected int[] startPoint;

    //结束点
    protected int[] endPoints;

    protected int flyFrame = 0;

    protected int[] flyPaintStart = new int[2];

    protected double targetDistance;
    protected String name;
    private String itemName;
    FCItem fcItem;
    private int endFrame = 5;
    private boolean throwToenemy = true;//默认是对敌人使用
    private boolean stopShowThrowItem = false;
    private boolean canHit = true;

    FCAnimation boomAni = null;
    int boomFrame = 0;
    int boomPngIndex = -1;
    int startFrame = 0;

    /**
     * 没特效的扔道具
     *
     * @param itemName
     * @param throwToenemy
     * @param name
     * @param startPoint
     * @param endpoint
     */
    public ThrowItemAni(String itemName, boolean throwToenemy, String name, int[] startPoint, int[] endpoint, boolean isPlayerAttackMode) {
        this.name = name;
        this.itemName = itemName;
        this.isPlayerAttackMode = isPlayerAttackMode;
        fcItem = itemLib.getItemOfID(itemName);
        this.startPoint = startPoint;
        this.endPoints = endpoint;
        //STEP 1计算俩点之间距离，速度假设是v像素/毫秒，
        int v = (int) (750 * fcv.getProportion());
        targetDistance = AnimationUtil.getDistance(startPoint, endpoint);
        flyFrame = (int) (16 * targetDistance / v);

        if (fcItem == null) {
            allFrames = 5 + endFrame;
        } else {
            boomAni = animationLib.getAnimationOfID(fcItem.getAttackAnimation());
            allFrames = flyFrame + endFrame;
            if (boomAni != null) {
                if (boomAni.getBoomPngUrl().size() > 0) {
                    boomFrame = boomAni.boomFrameTime / 16;
                    if (boomAni.getBoomPngUrl().size() > boomFrame) {
                        boomFrame = boomAni.getBoomPngUrl().size();
                    }
                    allFrames += boomFrame;
                }
            }
            allFrames = Math.max(5, allFrames);
        }

        if (isPlayerAttackMode) {
            playerOffset[0] = (int) (20 * fcv.getProportion());
            startPoint[0] -= playerOffset[0];
            startFrame = 8;
        } else {
            playerOffset[0] = (int) (40 * fcv.getProportion());
            startPoint[0] += playerOffset[0];
        }
        if (endpoint[0] == 0 || endpoint[0] >= (int) (1920 * fcv.getProportion())) {
            canHit = false;
        }
        attackReadyState = true;//举手动画
        allFrames += startFrame;
    }


    public void nextFrame(int frame) {
        //TODO 在这里必须计算好paintPoints 和startPngIndex这些
        if (fcItem != null) {
            if (frame <= startFrame && startFrame != 0) {
                flyPaintStart[0] = startPoint[0];
                flyPaintStart[1] = startPoint[1];
            } else {
                if (frame < (flyFrame + startFrame)) {
                    attackReadyState = false;//举手动画
                    AnimationUtil.getAnimatePoint(flyPaintStart, startPoint, endPoints, (float) (frame - startFrame) / flyFrame);
                } else {
                    stopShowThrowItem = true;
                    if (boomFrame != 0 && frame >= (startFrame + flyFrame) && frame <= (allFrames - endFrame)) {
                        int p = boomFrame / boomAni.getBoomPngUrl().size();
                        int index = (frame - flyFrame - startFrame) / p + 1;
                        boomPngIndex = Math.min(index, boomAni.getBoomPngUrl().size() - 1);
                        boomPngIndex = Math.max(boomPngIndex, 0);

                        flyPaintStart[0] = endPoints[0] + (int) (boomAni.boomPngCenterOffsetX * fcv.getProportion());
                        flyPaintStart[1] = endPoints[1] + (int) (boomAni.boomPngCenterOffsetY * fcv.getProportion());
                        if(boomAni.getBoomPngScales() != 1) {
                            double dw = (boomAni.getBoomPngScales() - 1)*boomAni.boomPngSizeW * fcv.getProportion();
                            double dh = (boomAni.getBoomPngScales() - 1)*boomAni.boomPngSizeH * fcv.getProportion();
                            flyPaintStart[0] -= (int)(dw/2);
                            flyPaintStart[1] -= (int)(dh/2);
                        }
                    } else if (frame > (allFrames - endFrame)) {
                        boomPngIndex = -1;
                        //爆炸了，可以将敌人闪俩下
                        int cha = frame - (startFrame + flyFrame + boomFrame);
                        if (throwToenemy && canHit) {//被打中才闪
                            if (cha < endFrame / 4) {
                                beHitShowVisable = false;
                            } else if (cha < endFrame / 2) {
                                beHitShowVisable = true;
                            } else if (cha < 3 * endFrame / 4) {
                                beHitShowVisable = false;
                            } else {
                                beHitShowVisable = true;
                            }
                        }
                    }
                }
            }
        }
    }


    @Override
    public void paintBattleSceen(Graphics g, FCRMaster fcrMaster) {
        if (fcItem != null) {
            if (boomPngIndex == -1) {
                if (!stopShowThrowItem) {
                    flying(g, fcrMaster);
                }
            } else {
                boom(g, fcrMaster);
            }
        }
    }


    @Override
    public void fire(Graphics g, FCRMaster fcrMaster) {
    }

    @Override
    public void flying(Graphics g, FCRMaster fcrMaster) {
        g.drawImage(fcItem.getPngPng(), flyPaintStart[0], flyPaintStart[1],
                (int) (32 * fcv.getProportion()), (int) (32 * fcv.getProportion()), fcrMaster);
    }

    @Override
    public void boom(Graphics g, FCRMaster fcrMaster) {
        if (boomPngIndex != -1) {
            g.drawImage(boomAni.getBoomPng().get(boomPngIndex), flyPaintStart[0], flyPaintStart[1],
                    (int) (boomAni.getBoomPngScales() * boomAni.boomPngSizeW * fcv.getProportion()),
                    (int) (boomAni.getBoomPngScales() * boomAni.boomPngSizeH * fcv.getProportion()), fcrMaster);
        }
    }
}
